package com.api.workflow.web;

import weaver.common.util.string.StringUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.Cell;
import weaver.soa.workflow.request.DetailTable;
import weaver.soa.workflow.request.RequestInfo;
import weaver.soa.workflow.request.Row;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * MMI005 跨公司调拨订单提报【OA-》SAP】
 * */
public class YNMMI005GXAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        try {
            writeLog("start------------------------YNMMI005GXAction");
            //获得流程id
            String workflowid = requestInfo.getWorkflowid();
            //获取表id
            String requestid = requestInfo.getRequestid();
            writeLog("start------------------------YNMMI005GXAction" + requestid);
            RecordSet rs = new RecordSet();
            rs.execute("select tablename from workflow_base a,workflow_bill b where a.formid=b.id and a.id=" + workflowid);
            rs.next();
            //获取主表名
            String maintable = Util.null2String(rs.getString("tablename"));

            //根据表id和主表名查找主表的相关信息
            rs.executeQuery("select * from " + maintable + " where requestid=?", new Object[]{requestid});
            rs.next();
            String lcbh = Util.null2String(rs.getString("lcbh"));//流程编号
            RecordSet recordSet = new RecordSet();
            if (!StringUtil.isNullOrEmpty(lcbh)){
                rs.executeQuery("select * from uf_cgddxx where lcbh=? ",lcbh);
                while (rs.next()){
                    String cgpzh = Util.null2String(rs.getString("cgpzh"));
                    String id = Util.null2String(rs.getString("id"));
                    String xh = Util.null2String(rs.getString("xh"));
                    writeLog("cgpzhcgpzhcgpzh"+cgpzh);
                    writeLog("ididid"+id);
                    writeLog("xhxhxh"+xh);
                    recordSet.executeUpdate("update uf_cgddxx set cgddzj=? where id=?",cgpzh+xh,id);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager().setMessagecontent("接口出错,请联系管理员!" + e.getMessage());
        }
        return Action.SUCCESS;
    }


    /**
     * 提取明细表全部的数据
     * @param requestInfo
     * @return
     */
    private static List<Map<String, Object>> getMaps(RequestInfo requestInfo) {
        DetailTable[] detailTable = requestInfo.getDetailTableInfo().getDetailTable();
        //这里是存储了全部的明细表数据  可自行修改存储结构
        List<Map<String, Object>> detailTableMap = new ArrayList<>();
        if (detailTable.length > 0) {
            for (int i = 0; i < detailTable.length; i++) {
                DetailTable dt = detailTable[i];
                Row[] s = dt.getRow();
                for (int j = 0; j < s.length; j++) {
                    Row r = s[j];
                    Cell c[] = r.getCell();
                    Map<String, Object> objectObjectHashMap = new HashMap<>();
                    for (int k = 0; k < c.length; k++) {
                        Cell c1 = c[k];
                        String name = c1.getName().toLowerCase();
                        String value = c1.getValue();
                        objectObjectHashMap.put(name, value);
                    }
                    detailTableMap.add(objectObjectHashMap);
                }
            }
        }
        return detailTableMap;
    }


}